package com.lab.rx;

import android.view.View;

import com.jakewharton.rxbinding.widget.TextViewTextChangeEvent;

/**
 * RxJava 监听目标EditText为空时，显示另一个View
 * Created by MiJack on 2015/12/17.
 */
public class TextChangeTargetVisibilitySubscriber extends BaseSubscriber<TextViewTextChangeEvent> {
    private View target;
    /**
     * 正相关与反相关
     */
    public boolean isPositive = true;

    public TextChangeTargetVisibilitySubscriber setTarget(View target) {
        return setTarget(target, true);
    }


    public TextChangeTargetVisibilitySubscriber setTarget(View target, boolean isPositive) {
        this.target = target;
        this.isPositive = isPositive;
        return this;
    }

    @Override
    public void onNext(TextViewTextChangeEvent event) {
        boolean enable = event.text().length() > 0;
        if (isPositive){
            target.setVisibility(enable ? View.VISIBLE : View.INVISIBLE);
        }else{
            target.setVisibility(enable ? View.INVISIBLE : View.VISIBLE );
        }
        target.setEnabled(enable);
    }

}
